package manageFriendshipsServlets;

import java.io.IOException;
import java.util.ArrayList;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sessions.friendshipSession.FriendshipSessionRemote;
import sessions.requestFriendshipSession.FriendshipRequestSessionRemote;
import entities.Friendship;
import entities.FriendshipRequest;

public class UserFriendshipsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		drawPage(request, response);
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		drawPage(request, response);
	}

	private void drawPage(HttpServletRequest request,
			HttpServletResponse response) {
		ArrayList<Friendship> friendships;
		ArrayList<FriendshipRequest> friendshipReceived, friendshipSent, indirectFriendshipReceived, indirectFriendshipSent, suggestion;
		Context jndiCtx;
		try {
			jndiCtx = new InitialContext();
			FriendshipSessionRemote friendshipSession = (FriendshipSessionRemote) jndiCtx
					.lookup("Progetto/FriendshipSession/remote");
			FriendshipRequestSessionRemote friendshipRequestRemote = (FriendshipRequestSessionRemote) jndiCtx
					.lookup("Progetto/FriendshipRequestSession/remote");
			// cerca tutte le amicizie
			int idActualUser = (Integer) request.getSession()
					.getAttribute("id");
			friendships = friendshipSession.getUserFriendships(idActualUser);
			suggestion = friendshipRequestRemote
					.ReceveidRequests(idActualUser,false,true);
			
			friendshipReceived = friendshipRequestRemote
					.ReceveidRequests(idActualUser,true,false);
			indirectFriendshipReceived = friendshipRequestRemote
					.ReceveidRequests(idActualUser,false,false);
			friendshipSent = friendshipRequestRemote.SentRequests(idActualUser,true,false);
			indirectFriendshipSent = friendshipRequestRemote.SentRequests(idActualUser,false,false);
			//unisco le richieste di amicizia
			friendshipSent=union(friendshipSent,indirectFriendshipSent);
			friendshipReceived=	union(friendshipReceived,indirectFriendshipReceived);
			request.getSession().setAttribute("friendshipSent", friendshipSent);
			request.getSession().setAttribute("friendshipReceived", friendshipReceived);
			//setto i parametri della sessione
			request.getSession().setAttribute("userFriends", friendships);
			request.getSession().setAttribute("suggestion", suggestion);
			response.sendRedirect("/ProgettoWeb/RegisteredUser/FriendsList.jsp");
		} catch (Exception e) {
			System.out.print("errore nella connessione remota");
			e.printStackTrace();
		}
	}
	
	private ArrayList<FriendshipRequest> union (ArrayList<FriendshipRequest> r1, ArrayList<FriendshipRequest> r2){
		if(r1==null || r1.size()==0 ){
			return r2;
		}
		if(r2==null || r2.size()==0){
			return r1;	
		}
		r1.addAll(r2);
		return r1;
	}
}
